import 'package:flutter/material.dart';
import 'dart:ui';

class Adapt {
  static var ratio;
  static int _uiWidth = 0;
  // 物理像素和设计像素比，用多少物理尺寸渲染设计尺寸
  // static double _pixelRatio = MediaQueryData.fromWindow(window).devicePixelRatio;
  static init({int uiWidth = 750}) {
    if (_uiWidth == 0) _uiWidth = uiWidth;
    ratio = sw / _uiWidth;
  }

  static get sw => MediaQueryData.fromWindow(window).size.width;
  static get sh => MediaQueryData.fromWindow(window).size.height;
  static get stop => MediaQueryData.fromWindow(window).padding.top;
  static get sbtm => MediaQueryData.fromWindow(window).padding.bottom;
  static get txtScl => MediaQueryData.fromWindow(window).textScaleFactor;
}

extension IntExtension on int {
  get px {
    if (Adapt.ratio == null || Adapt.ratio == 0) Adapt.init();
    return this * Adapt.ratio;
  }

  get fpx {
    if (Adapt.ratio == null || Adapt.ratio == 0) Adapt.init();
    return this * Adapt.ratio;
  }

  get vw {
    return (this / 100) * Adapt.sw;
  }

  get vh {
    return (this / 100) * Adapt.sh;
  }
}
